<?php
/******************************************************************************
 master_state.php
 Created on 18.01.2007, by Moorthy
 Purpose:
         To enter the Master state Details
/*****************************************************************************/
 
include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."pagesettings/master.inc");
include(DOCINCLUDES."global_files/global_js_form.inc");
include(DOCINCLUDES."global_files/global_functions.inc");
include(DOCINCLUDES."global_files/global_report.inc");

// include(DOCINCLUDES."global_files/global_errorpage.inc");
$action=$_GET["action"];
global $company_id;
$page->header_path="../../";

// $form=new cForm();
// $db=new cDatabase($state_id);
// $sql=new pgSql();
$report=new cReport();
$dv=new cDataValidation();
$db->setSearchPath();
if($_POST)
	{
	$dv->validations["state_code"] = array("isEmpty"=>"Enter Code");
	$dv->validations["state_name"] = array("isEmpty"=>"Enter State Name");
$columns=array("state_code","state_name");
$selected_values = getFilledRows($_POST,$columns);
if(!$selected_values)
		{
                $dv->errors["general"] = "<font color=\"red\">Enter Atleast One Record</font>";
		}
	if(!$dv->errors)
		{
		$dv->errors = validateArrayValues($selected_values);
		}
	if(!$dv->errors)
		{
		$db->beginTransaction();
		foreach($selected_values as $key => $value)
                        {
			$state_details["state_code"] = $_POST["state_code"][$key];
			$state_details["state_name"] = $_POST["state_name"][$key];
			$sql->table="_100351._mstate";
			$sql->column=$state_details;
			$sql->generateInsertQuery();
			$db->query = $sql->query;
			$db->runQuery();
			}
		if($db->result)
			{
			$db->endTransaction();
			unset($_POST);
			$action="view_details";
			}
		}
	}
$action=($action)?$action:"view_details";
$page->title=$page->heading="State Details";
$page->doHeaderHtml();
echo "<div align=\"center\">";
if($_GET["rtype"]!="p")
	{
	if($action=="add_details")
		{
		$report->query_string=array("action"=>"view_details");
		echo $report->returnLink($PHP_SELF,"View Details");
		}
	else
		{
		$report->query_string=array("action"=>"add_details");
		echo $report->returnLink($PHP_SELF,"Add Details");
		}
	}
if($action=="add_details")
	{
	$form->properties=array("name"=>"state_master","method"=>"POST","action"=>$PHP_SELF);
	echo $form->startForm();
		for($i=0;$i<10;$i++)
			{
			$form->properties=array("type"=>"text","name"=>"state_code[$i]","value"=>"{$_POST[state_code][$i]}","size"=>"15");
			$state_detail_array[$i]["Code"]=$form->generateInputTag();
			$state_detail_array[$i]["Code"].= $dv->errors[$i]["state_code"];

			$form->properties=array("type"=>"text","name"=>"state_name[$i]","value"=>"{$_POST[state_name][$i]}","size"=>"35");
			$state_detail_array[$i]["State Name"]=$form->generateInputTag();
			$state_detail_array[$i]["State Name"].= $dv->errors[$i]["state_name"];
			}
	$report->customization["table_style"]="width:40%; border-style:solid; border-width:thin; border-collapse:collapse;";
	$report->title="State Entry";
	$report->highlight=true;
	$report->data = $state_detail_array;
	$report->doHorizontalTable();
	$form->properties=array("type"=>"submit","name"=>"action","value"=>"Save","accesskey"=>"3");
	echo $form->generateInputTag();
	echo $form->endForm();
	}
else
	{
	// To display the existing master state details
	$db->query="SELECT state_code as \"Code\",state_name as \"state\" from _mstate order by state_code;";
	$db->runQuery();
	$state_detail_array=$db->returnArrays();
	$report->customization["table_style"]="width:60%; border-style:solid; border-width:thin; border-collapse:collapse;";
	$report->customization["tr_style"]="border-style:none; border-width:thin; border-collapse:collapse";
	$report->title="State Details";
	$report->highlight=true;
	$report->data=$state_detail_array;
	$report->setNA();
	$report->doHorizontalTable();
	}
echo"</div>";
if($action=="add_details")
	$page->doFooterHtml();
else
	{
	$report->query_string=array("action"=>"view_details");
	$page->report=$report;
	$page->doPrintBottomHtml();
	}
?>
